﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace AcctrueWMS.Foundation.Data
{
    public class DataModelCollection : InternalDataCollectionBase
    {
        private DataModel dataModel = null;
        public DataModel OwnerModel
        {
            get
            {
                return this.dataModel;
            }
            set
            {
                this.dataModel = value;
            }
        }
        public DataModelCollection(DataModel Owner)
        {
            dataModel = Owner;
        }
        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="dataModel"></param>
        public void Add(DataModel dataModel)
        {
            //if (!this.List.Contains(dataModel))
            //{
            dataModel.Parent = OwnerModel;
            List.Add(dataModel);
            //}
            //else
            //{
            //    throw new Exception("该实体在集合中已存在");
            //}
        }
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="dataModel"></param>
        public void Remove(DataModel dataModel)
        {
            List.Remove(dataModel);
        }
        /// <summary>
        /// 移除指定位置的行
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }
        /// <summary>
        /// 清除所有集合
        /// </summary>
        public void Clear()
        {
            this.List.Clear();
        }
        /// <summary>
        /// 属性访问器
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DataModel this[int index]
        {
            get
            {
                if (List.Count > index)
                    return List[index] as DataModel;
                else
                    return null;
            }
        }

        public DataModel this[string name]
        {
            get
            {
                foreach (DataModel model in List)
                {
                    if (model.Name == name)
                        return model;
                }
                return null;
            }
        }
        /// <summary>
        /// 增加行集合
        /// </summary>
        /// <param name="dataModels"></param>
        public void AddRange(DataModel[] dataModels)
        {
            foreach (DataModel dm in dataModels)
            {
                dm.Parent = OwnerModel;
            }
            this.List.AddRange(dataModels);
        }
        private ArrayList list;
        protected override System.Collections.ArrayList List
        {
            get
            {
                if (list == null)
                    list = new System.Collections.ArrayList();
                return list;
            }
        }
        /// <summary>
        /// 获取行值
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int IndexOf(DataModel model)
        {
            int index = -1;
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i] == model)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }


    }
}
